package vn.edu.hut.soict.translate.database.tool;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import vn.edu.hut.soict.translate.database.TranslatorDatabase;
import vn.hus.nlp.tokenizer.DefaultTokenizerFactory;
import vn.hus.nlp.tokenizer.Token;
import vn.hus.nlp.tokenizer.Tokenizer;

public class ExtractVietnameseSentence {
	private static final String DEFAULT_RESOURCE_DIRECTORY = "res";

	private TranslatorDatabase database;
	private DefaultTokenizerFactory tokenizerFactory;

	public ExtractVietnameseSentence() {
		database = new TranslatorDatabase();
		try {
			tokenizerFactory = new DefaultTokenizerFactory(DEFAULT_RESOURCE_DIRECTORY);
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		database.connect();
	}

	public void writeToFile(String path) {
		List<String> sentences = database.getAllVietnameseSentences();
		BufferedWriter writer = null;

		try {
			writer = new BufferedWriter(new FileWriter(path));
			for (String sentence : sentences) {
				Tokenizer tokenizer = tokenizerFactory.getTokenizer(sentence);
				while (true) {
					Token token = tokenizer.next();
					String tokenText = token.getText().replace(' ', '_');
					writer.write(tokenText);
					if (tokenizer.hasNext()) {
						writer.write(" ");
					}
					else {
						break;
					}
				}
				writer.newLine();

			}
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			if (writer != null) {
				try {
					writer.close();
				}
				catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		ExtractVietnameseSentence extrator = new ExtractVietnameseSentence();
		extrator.writeToFile("res\\sentences.txt");
	}
}
